package com.netwrok;

import java.io.IOException;

import com.ex.common.ObjectPool;
import com.netwrok.mem.IoEventPool;

public class IoEvent implements Runnable {
	static final int Type_Net_New = 0;
	static final int Type_Msg_Rcv = 1;
	static final int Type_Net_Cls = 2;
	int type;
	IoSession session;
	Object arg;

	NetworkEvents networkEvents = null;

	public IoEvent() {
	}

	public void reset(int type, IoSession session, Object arg,
			NetworkEvents network) {
		this.type = type;
		this.session = session;
		this.arg = arg;
		networkEvents = network;
	}

	@Override
	public void run() {
		switch (type) {
		case Type_Net_New:
			networkEvents.handler.onNewSession(session);
			break;

		case Type_Net_Cls:
			networkEvents.queues.removeSession4Response(session);
			try {
				session.channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			networkEvents.handler.onClose(session);
			break;
		case Type_Msg_Rcv:
			networkEvents.handler.onRcvMsg(session, (RequestMsg) arg);
			break;
		}
		ObjectPool<IoEvent> pool = IoEventPool.getPool();
		pool.free(this);
	}
}